package com.dgyt.contract.msg.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgyt.contract.config.ResponseBean;
import com.dgyt.contract.config.base.BaseController;
import com.dgyt.contract.config.base.BaseSelectSymbol;
import com.dgyt.contract.config.entity.SymbolPlus;
import com.dgyt.contract.msg.entity.DgMsg;
import com.dgyt.contract.msg.entity.DgMsgUser;
import com.dgyt.contract.msg.service.IDgMsgService;
import com.dgyt.contract.msg.service.IDgMsgUserService;
/*import com.dgyt.contract.websocket.WebSocketServer;*/
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author SQ
 * @since 2020-11-05
 */
@RestController
@RequestMapping("/msg")
public class DgMsgController  extends BaseController<DgMsg, IDgMsgService> {


    @Autowired
    private IDgMsgUserService userService;
 /*   @Autowired
    private WebSocketServer webSocketServer;*/
    /**
     * 插入消息 并向其他人员推送
     * @param msg
     * @return
     */
    @RequestMapping("save")
    public ResponseBean save(@RequestBody DgMsg msg){
        msg.setFlag(1).setMsgTime(LocalDateTime.now());
        boolean add = super.add(msg);
//        try {
//            webSocketServer.sendMessage(JSONObject.toJSONString(msg));
//        } catch (IOException e) {
//             add = super.del(msg.getId());
//            e.printStackTrace();
//        }

        return new ResponseBean(0,"已接受",add);
    }

    /**
     * 删除消息
//     * @param id
     * @return
     */
    @RequestMapping("delete")
    public ResponseBean delete(@RequestBody HashMap<String,String> map){
        return new ResponseBean(0,"已接受",super.del(map.get("id")));
    }

    /**
     * 根据条件查询消息
     * @param map
     * @return
     */
    @RequestMapping("query")
    public ResponseBean query(@RequestBody HashMap<String,String> map){
        IPage<DgMsg> dg_msgIPage = super.base_get_list_by_page(map);
        return new ResponseBean(0,"查询完毕",dg_msgIPage);
    }

    /**
     * 用户确认消息
//     * @param msgid 消息di
//     * @param loginName 用户名称
     * @return 确认结果
     */
    @RequestMapping("user_read")
    public ResponseBean user_read(@RequestBody HashMap<String,String> map){
        DgMsgUser user_msg = new DgMsgUser().setLoginName(map.get("loginName")).setMsgId(Integer.parseInt(map.get("msgid"))).setViewTime(LocalDateTime.now()).setUserVew("已读");
        boolean save = userService.save(user_msg);
        return new ResponseBean(0,"确认完毕",save);
    }

    /**
     * 查询用户已读消息
     * @param map
     * @return
     */
    @RequestMapping("query_read")
    public ResponseBean query_read(@RequestBody HashMap<String,String> map){
//        SymbolPlus symbolPlus = BaseSelectSymbol.select_Analysis(map);
        Page<DgMsg> dg_msgPage = new Page<DgMsg>(Long.parseLong(map.get("pageindex")),Long.parseLong(map.get("pagesize")));
        IPage<DgMsg> login_name = super.sService.read(dg_msgPage, map.get("loginName"));
        return new ResponseBean(0,"查询完毕",login_name);

    }
    /**
     * 查询用户已未读消息
     * @param map
     * @return
     */
    @RequestMapping("query_unread")
    public ResponseBean query_unread(@RequestBody HashMap<String,String> map){
//        SymbolPlus symbolPlus = BaseSelectSymbol.select_Analysis(map);
        Page<DgMsg> dg_msgPage = new Page<DgMsg>(Long.parseLong(map.get("pageindex")),Long.parseLong(map.get("pagesize")));
        IPage<DgMsg> login_name = super.sService.unread(dg_msgPage, map.get("loginName"));
        return new ResponseBean(0,"查询完毕",login_name);
    }


    /**
     * 首页获取未读数与重要消息
     * @param map
     * @return
     */
    @RequestMapping("check")
    public ResponseBean check(@RequestBody HashMap<String,String> map){
        System.out.println(map.get("loginName"));
        Integer size = super.sService.unread_size(map.get("loginName"));
        List<DgMsg> list = sService.check(map.get("loginName"));
        HashMap<String, Object> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("unreadsize",size);
        stringStringHashMap.put("important",list);
        return new ResponseBean(0,"查询完毕",stringStringHashMap);
    }

}
